﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace ALDIOrganizer
{
    public class DataGridViewPopup : DataGridView
    {
        private bool _rightSidePreferred;
        public bool RightSidePreferred
        {
            get
            {
                return _rightSidePreferred;
            }
            set
            {
                _rightSidePreferred = value;
            }
        }        
        private bool _bottomPreferred;        
        public bool BottomPreferred
        {
            get
            {
                return _bottomPreferred;
            }
            set
            {
                _bottomPreferred = value;
            }
        }

        /// <summary>
        /// Places
        /// </summary>
        /// <param name="pColumn">
        /// cell column index
        /// </param>
        /// <param name="pRow">
        /// cell row index
        /// </param>
        /// <param name="pDialogForm">
        /// form to be displayed as dialog form
        /// </param>
        /// <param name="pMarginPixels">
        /// distance to be left between the corner of the form and the corner of the cell
        /// </param>
        /// <returns></returns>
        public DialogResult ShowDialogAt(
            int pColumn,
            int pRow,
            Form pDialogForm,
            int pMarginPixels)
        {
            LocateForm(pColumn, pRow, pDialogForm, pMarginPixels);
            return pDialogForm.ShowDialog();
        }

        protected void LocateForm(int pCol, int pRow, Form pForm, int pMargin)
        {            
            if (pCol < 0 ||
                pRow < 0 ||
                pCol > Columns.Count - 1 ||
                pRow > Rows.Count - 1 ||                
                pMargin < 0)
            {
                throw new ArgumentOutOfRangeException();
            }
            if (pForm == null)
            {
                throw new ArgumentNullException();
            }            
            var cellRectangle = GetCellDisplayRectangle(pCol, pRow, false);
            cellRectangle.Location = PointToScreen(cellRectangle.Location); // its absolute position
                        
            /* it will be over 4 times bigger than the actual form, as it covers all
             * possible locations of the dialog form; above, below and both
             * on the left and right side of the cell */
            var consideredTerritory = new Rectangle();
            consideredTerritory.X = cellRectangle.Left - pForm.Width;
            consideredTerritory.Width = (pForm.Width * 2) + cellRectangle.Width;
            consideredTerritory.Y = cellRectangle.Top - pForm.Height;
            consideredTerritory.Height = (pForm.Height * 2) + cellRectangle.Height;

            // on which side does our "considered territory" NOT extend beyond the edge of the screen?
            var isSpaceOnTheLeft = consideredTerritory.X >= 0;
            var isSpaceOnTheRight = consideredTerritory.Right <= Screen.PrimaryScreen.WorkingArea.Width;
            var isSpaceOnTheTop = consideredTerritory.Y >= 0;
            var isSpaceBelow = consideredTerritory.Bottom <= Screen.PrimaryScreen.WorkingArea.Height;

            // by default, we locate pop-up windows on the right side of the cell
            
            if (RightSidePreferred & isSpaceOnTheRight)
            {
                pForm.Left = cellRectangle.Right - pMargin;
            }
            else if (isSpaceOnTheLeft)
            {
                pForm.Left = consideredTerritory.Left + pMargin;
            }            

            // now the same procedure, only vertically.
            if (isSpaceBelow)
            {
                pForm.Top = cellRectangle.Bottom - pMargin;
            }
            else if (isSpaceOnTheTop)
            {
                pForm.Top = consideredTerritory.Top + pMargin;
            }
        }
    }
}
